package com.sunjob.mroassets.mapper;

import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import com.sunjob.common.domain.assets.Status;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface StatusMapper {


 @Select("select a.part_id,a.part_name,a.status,b.instance_name from t_part a,t_instance b where a.instance_id=b.instance_id and a.status=#{status}")
    @Results({
            @Result(property = "partId",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "status",column = "status"),
            @Result(property = "message",column = "instance_name")

    })
    List<Part>getPartListByStatus(Integer status);


   @Select("select instance_id,instance_name,status from t_instance where status=#{status}")
   @Results({
           @Result(property = "instanceId",column = "instance_id"),
           @Result(property = "instanceName",column = "instance_name"),
           @Result(property = "status",column = "status")
   })
    List<Instance>getInstanceListByStatus(Integer status);



    //修改实例状态
    @Update("update t_instance set status = #{status} where instance_id = #{instanceId}")
    int updateInstanceStatus(Long instanceId,int status);


    //通过部件id将零件的使用寿命进行修改
    @Update("update t_accessory set last_time = time where part_id = #{partId}")
    void updateAccessoryTimeBypartId(Long partId);

    //维护实例列表中的实例状态
    @Update("update t_instance set status = 13 where time = 0  and (status != 13 or status is null)")
    void protectInstanceStatus();

    //维护部件列表中的部件状态
    @Update("update t_part set status = 22 where last_time = 0  and (status != 13 or status is null)")
    void updatePartStatus();

    //维护零件列表中的零件状态
    @Update("update t_accessory set status = 32 where last_time = 0  and (status != 13 or status is null)")
    void updateAccessoryStatus();

   //通过部件name和实例id将零件的使用寿命进行修改
    @Update("update t_accessory set last_time = time where part_id = " +
            "(select part_id from t_part where instance_id = #{instanceId} and part_name = #{partName})")
    void updateAccessoryTimeBypartName(String partName,Long instanceId);
}
